var jwt = require('jwt-simple');//token令牌
var config = require('config-lite');

var UserModel = require('../models/user');
var errPock = require('./errPack');
// module.exports = function (req, res, next) {
// 	console.log("请求了一个服务");
// 	next();
// };

module.exports = {
	get: function(req, res, next) {
		console.log("请求了一个get服务");
		if(!!req.query.token) {
			var token = req.query.token;
			try {
			    var decoded = jwt.decode(token, config.TokenSecret);
			    if (decoded.exp <= Date.now()) {
				  return errPock( res, 1004);
				}
			    UserModel.getUserSign({name: decoded.iss})
			    	.then(function (user) {
			    		req.user = user;
			    		next();
			    	})
			    	.catch(next);

			  } catch (err) {
			    return next(err);
			  }
		} else {
			next();
		}
	},
	post: function(req, res, next) {
		console.log("请求了一个post服务");
		next();
	}
}